﻿Imports System.ComponentModel
Imports System.Linq.Expressions
Imports WPFSearchDataGrid.EventAggregator

Namespace Assets

    Public Class TextBoxDataItem
        Implements IDataItem
        
        Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

#Region "   Attributes"

        Protected m_datapropertyname As String
        Protected m_value As Object
        Protected m_header As String
        Protected m_ieventaggregator As IEventAggregator = Nothing
        Protected m_templatename As String
        Public Property ItemsSource() As IEnumerable Implements IDataItem.ItemsSource

#End Region

#Region "   Properties"

#Region "   DataPropertyName"

        Public Property DataPropertyName As String Implements IDataItem.DataPropertyName
            Get
                Return m_datapropertyname
            End Get
            Set(value As String)
                m_datapropertyname = value
                OnNotifyPropertyChanged("Name")
            End Set
        End Property

#End Region

#Region "   Value"

        Public Property Value As Object Implements IDataItem.Value
            Get
                Return m_value
            End Get
            Set(value As Object)
                If Equals(m_value, value) Then Return
                m_value = If(String.IsNullOrEmpty(value), Nothing, value)
                m_ieventaggregator.PublishEvent(New DataItemValueChanged(Me))
                OnNotifyPropertyChanged("Value")
            End Set
        End Property

#End Region

#End Region

#Region "   Constructor"

        Public Sub New(column As DataGridColumn, publisher As IEventAggregator)
            Dim _dgbc As DataGridBoundColumn = TryCast(column, DataGridBoundColumn)
            Dim _b As Binding = _dgbc.Binding
            m_datapropertyname = _b.Path.Path
            m_header = column.Header
            Value = Nothing
            m_ieventaggregator = publisher
            m_templatename = "TextBoxTemplate"
        End Sub

#End Region

#Region "   FilterAsExpression"

        Public Function FilterAsExpression(Of T)(pi As Reflection.PropertyInfo) As Expression(Of Func(Of T, Boolean)) Implements IDataItem.FilterAsExpression
            Try
                Dim _parameterexpression = Expression.Parameter(GetType(T))
                Dim _memberxpression As MemberExpression = Expression.MakeMemberAccess(_parameterexpression, pi)
                'Dim tc As TypeConverter = TypeDescriptor.GetConverter(pi.PropertyType)

                Dim be As Expression = ExpressionFactory.Instance.Resolve(_memberxpression, Me, pi)

                'Dim va As Object = tc.ConvertFrom(m_value)
                'Dim _value As ConstantExpression = Expression.Constant(va, pi.PropertyType)
                'Dim be As Expression = Expression.Equal(_memberxpression, _value)
                Dim _lambda As Expression(Of Func(Of T, Boolean)) = Expression.Lambda(Of Func(Of T, Boolean))(be, New ParameterExpression() {_parameterexpression})
                Return _lambda
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

#End Region

#Region "   FilterAsString"

        Public Function FilterAsString(Of T)(pi As Reflection.PropertyInfo) As String Implements IDataItem.FilterAsString
            If pi.PropertyType Is GetType(String) Then
                Return String.Format("({0} LIKE '{1}*')", m_datapropertyname, m_value)
            Else
                Return String.Format("({0} = {1})", m_datapropertyname, m_value)
            End If
        End Function


#End Region

#Region "   OnNotifyPropertyChanged"

        Private Sub OnNotifyPropertyChanged(ByVal propertyname As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyname))
        End Sub

#End Region

#Region "   Reset"

        Public Sub Reset(refresh As Boolean) Implements IDataItem.Reset
            m_value = Nothing
            If refresh Then
                m_ieventaggregator.PublishEvent(New DataItemValueChanged(Me))
                OnNotifyPropertyChanged("Value")
            End If
        End Sub

#End Region

#Region "   HasItemsSource"

        Public ReadOnly Property HasItemsSource As Boolean Implements IDataItem.HasItemsSource
            Get
                Return False
            End Get
        End Property

#End Region

#Region "   TemplateName"

        Public ReadOnly Property TemplateName() As String Implements IDataItem.TemplateName
            Get
                Return m_templatename
            End Get
        End Property

#End Region



    End Class

End Namespace